-- TreeHouse
-- Create by cheny3
-- 树屋

TreeHouse = {
    classId = 0,
    skills  = {},

    -- 对象类型为树屋
    type = OBJECT_TYPE_TREE_HOUSE,
};
TreeHouse.__index = TreeHouse;

-- 构造函数
function TreeHouse.new(classId, data)
    local self = {};
    setmetatable(self, TreeHouse);
    self.classId = classId;
    self.dbase = Dbase.new(data);

    for key, value in pairs(data) do
        self[key] = value;
    end

    -- 如果有技能
    if data.skills ~= nil then
        self:setSkills(data.skills);
    else
        self:setSkills({});
    end

    return self;
end

-- 取得该对象的描述字符串
function TreeHouse:get_ob_id()
    return string.format("TreeHouse:%s", tostring(self.classId));
end

-- 查找数据
function TreeHouse:query(path, path2, default)
    if path == "classId" or path == "class_id" then
        return self.classId;
    else
        local v = self.dbase:query(path, path2, default);
        if v then
            return v;
        else
            return TreeHouseM.query(self.classId, path);
        end
    end
end

-- 树屋技能
function TreeHouse:getSkills()
    -- 所有后天学习的技能
    return self.skills;
end

-- 树屋所有技能
function TreeHouse:getAllSkills()
    return self:getSkills();
end

-- 名称
function TreeHouse:getName()
    return self:query("name");
end

-- 需要保存的数据
function TreeHouse:getSaveData()
    return {
        ["active"] = self.active,
        ["action"] = self.action,
        ["next_skill"] = self:query("next_skill"),
        ["class_id"] = self.classId,
        ["skills"] = self:getAllSkills(),
        ["prop"] = self:query("prop"),
    };
end

-- 更新数据
function TreeHouse:updateData(data)
    for key, value in pairs(data) do
        self[key] = value;
    end

    self.dbase:aborb(data);
end

-- 是否已经被激活了
function TreeHouse:isActive()
    return self.active == 1;
end

-- 获取属主
function TreeHouse:getOwner()
    if self:isActive() then
        return ME.user;
    end
end

-- 获取位置
function TreeHouse:getPos()
    return self.pos or self.dbase:query("pos");
end

-- 倒计时
function TreeHouse:getCountDown()
    return TreeHouseM.getAttackCountDown(self);
end

-- 是否有技能
function TreeHouse:ownSkill(skillId)
    if self.skills[skillId] then
        return true;
    end
    return false;
end

-- 技能等级
function TreeHouse:getSkillLevel(skillId)
    if self.skills[skillId] then
        return self.skills[skillId];
    end
    return 0;
end

-- 是否是树屋
function TreeHouse:isTreeHouse()
    return true;
end

-- 获取当前的攻击范围
function TreeHouse:getAttackRange(desc)
    local minAttack = MAX_VALUE;
    local maxAttack = 0;

    for skillId, level in pairs(self.skills) do
        -- 如果不是攻击道具不管
        if TreeHouseM.querySkill(skillId, "attack_property") == 1 then
            local args = FormulaM.invoke("CALC_TREE_HOUSE_PROPERTY_ARGS", self, skillId, level, desc);
            local showAttack = FormulaM.invoke("CALC_TREE_HOUSE_ITEM_ATTACK", args);

            if showAttack > maxAttack and showAttack ~= 0 then
                maxAttack = showAttack;
            end

            if showAttack < minAttack and showAttack ~= 0 then
                minAttack = showAttack;
            end
        end
    end

    return {minAttack, maxAttack};
end

-- 设置技能
function TreeHouse:setSkills(skills)
    self.skills = skills;
end

-- 升级技能，这里不做检查
function TreeHouse:upgradeSkill(skillId)
    self.skills[skillId] = tonumber(self.skills[skillId]) + 1;
end

-- 学习技能
function TreeHouse:learnSkill(skillId)
    self.skills[skillId] = tonumber(self.skills[skillId]) + 1;
end
